package com.guizi.halftimesroom.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.guizi.halftimesroom.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Alan on 2016/7/15.
 */
public class DiscoverBnanerAdapter extends PagerAdapter {
    private List<ImageView> mLIst ;
    private Context mContext;
    private List<String> list;

    public DiscoverBnanerAdapter(List<ImageView> mLIst, Context mContext,List<String> list) {
        this.mLIst = mLIst;
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public int getCount() {
        Log.i("DiscoverBannerView","mLIst.size()-->"+mLIst.size());
        return mLIst == null?0:Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.i("DiscoverBannerView","mLIst.size()1111111-->"+mLIst.size());
        if(mLIst.size()!=0) {
            position = position % mLIst.size();
        }

        ImageView imageView = mLIst.get(position);
        Picasso.with(mContext).load(list.get(position))
                .error(R.mipmap.loading_error)
                .placeholder(R.mipmap.loading)
                .into(imageView);
        if(imageView.getParent() == null){
            container.addView(imageView);
        }
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        position = position % mLIst.size();
        View view = mLIst.get(position);
        container.removeView(view);
    }
}
